<html>
<head><meta charset="utf-8"><title>let ... else side discussion (let ... else vs if let) · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html">let ... else side discussion (let ... else vs if let)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228692272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692272">(Mar 04 2021 at 00:09)</a>:</h4>
<p>it's basically <code>unwrap_or</code>, but more flexible</p>



<a name="228692363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692363">(Mar 04 2021 at 00:10)</a>:</h4>
<p>I don't think I'd put it in the same category as unwrap_or. unwrap_or is for when there's a default. This is for when there's no default and you need to do error handling instead.</p>



<a name="228692423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692423">(Mar 04 2021 at 00:11)</a>:</h4>
<p>well you can use it for error handling, or you can use it for picking a default, or you can use it for whatever you like. "but more flexible"</p>



<a name="228692501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692501">(Mar 04 2021 at 00:11)</a>:</h4>
<p><code>let ... else</code> doesn't apply to the "picking a default" case at all, because it doesn't bind the variable in the <code>else</code> case.</p>



<a name="228692572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692572">(Mar 04 2021 at 00:12)</a>:</h4>
<p>oh that's.... entirely not how i imagined it to work.</p>



<a name="228692623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692623">(Mar 04 2021 at 00:12)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> Are you perhaps thinking of another construct somehow? <code>if let ... { } else { ... }</code> already works today and does what you'd expect it to do.</p>



<a name="228692702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692702">(Mar 04 2021 at 00:13)</a>:</h4>
<p>This is <code>let Some(x) = an_option else { diverge and don't reach the end of this block, most commonly via return or break }</code>.</p>



<a name="228692878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228692878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228692878">(Mar 04 2021 at 00:15)</a>:</h4>
<p>Okay, so... okay... so i don't use if let often i'm a match sort of fellow I wandered in from Haskell-town, and it turns out you can already write</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="mi">12</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>and you can <em>also</em> already just choose to diverge in the <code>else</code> path if you want to, so I guess i'm not clear on whatever the new thing would allow compared to this existing ability.</p>



<a name="228693000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693000">(Mar 04 2021 at 00:16)</a>:</h4>
<p>like instead of 12, i could have written <code>return</code> and that compiles</p>



<a name="228693031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693031">(Mar 04 2021 at 00:16)</a>:</h4>
<p>I believe primarily not having to bind-return-bind on the success case.</p>



<a name="228693073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693073">(Mar 04 2021 at 00:17)</a>:</h4>
<p>Right. And thus having to write the name of the thing three times.</p>



<a name="228693161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693161">(Mar 04 2021 at 00:18)</a>:</h4>
<p><code>let name = if let Some(name) = expr { name } else { ... }</code> is possible today, and it feels clunky and awkward. This is what people already do today.</p>



<a name="228693192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693192">(Mar 04 2021 at 00:18)</a>:</h4>
<p>so what would the new thing be</p>



<a name="228693197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693197">(Mar 04 2021 at 00:18)</a>:</h4>
<p>(wild bikeshed allowed for this answer)</p>



<a name="228693198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693198">(Mar 04 2021 at 00:18)</a>:</h4>
<p><code>let Some(name) = expr else { ...}</code></p>



<a name="228693235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693235">(Mar 04 2021 at 00:19)</a>:</h4>
<p>ohhhh, okay, i merged your first two posts in my head</p>



<a name="228693238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693238">(Mar 04 2021 at 00:19)</a>:</h4>
<p>okay i follow</p>



<a name="228693268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693268">(Mar 04 2021 at 00:19)</a>:</h4>
<p>/me is going to split this tangent into another topic for clarity.</p>



<a name="228693285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693285">(Mar 04 2021 at 00:19)</a>:</h4>
<p>requiring that the else block diverses seems… pretty weird to me. Not from the perpective of why you'd require but from the perspective of this seeming fairly constrained to add additional syntax over.</p>



<a name="228693415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/let%20...%20else%20side%20discussion%20%28let%20...%20else%20vs%20if%20let%29/near/228693415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/let.20.2E.2E.2E.20else.20side.20discussion.20(let.20.2E.2E.2E.20else.20vs.20if.20let).html#228693415">(Mar 04 2021 at 00:20)</a>:</h4>
<p>so it's like enhanced <code>?</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>